/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.model.Department;
import com.cmcc.zysoft.infocollection.service.DepartmentPcService;
import com.cmcc.zysoft.spring.security.model.User;
import com.cmcc.zysoft.spring.security.util.SecurityContextUtil;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: DepartmentController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-5 上午9:44:59
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("/pc/department")
public class DepartmentController extends BaseController {

	/**
	 * 属性名称: departmentPcService.
	 * 类       型: DepartmentPcService.
	 */
	@Resource
	private DepartmentPcService departmentPcService;
	
	/**
	 * 部门列表页面.
	 * @return String
	 */
	@RequestMapping("/main.htm")
	public String main() {
		return "/pc/department/list";
	}
	
	/**
	 * 获取部门树-部门管理列表展示.
	 * @param companyId 企业ID
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/tree.htm")
	@ResponseBody
	public List<Map<String, Object>> tree(String companyId) {
		return this.departmentPcService.tree(companyId);
	}
	
	/**
	 * 添加部门页面.
	 * @return String
	 */
	@RequestMapping("/add.htm")
	public String add() {
		return "/pc/department/add";
	}
	
	/**
	 * 保存部门信息.
	 * @param department 部门信息
	 * @return String
	 */
	@RequestMapping("/saveDepartmentInfo.htm")
	@ResponseBody
	@Transactional
	public String saveDepartment(Department department) {
		User user = SecurityContextUtil.getCurrentUser();
		if(!"0".equals(department.getParentDepartmentId())) { //选择了父部门
			//设置父部门有子部门
			Department dept = this.departmentPcService.getEntity(department.getParentDepartmentId());
			dept.setHaveChildDeparment("Y");
			this.departmentPcService.updateEntity(dept);
		} else { //没有选择父部门
			department.setParentDepartmentId("0");
		}
		department.setHaveChildDeparment("N");
		department.setDelFlag("0");
		department.setCreateMan(user.getUserId());
		department.setCreateTime(new Date());
		this.departmentPcService.insertEntity(department);
		return "SUCCESS";
	}
	
	/**
	 * 编辑部门信息页面.
	 * @param modelMap modelMap
	 * @param departmentId 部门ID
	 * @return String
	 */
	@RequestMapping("/edit.htm")
	public String edit(ModelMap modelMap, String departmentId) {
		Department department = this.departmentPcService.getEntity(departmentId);
		modelMap.addAttribute("department", department);
		return "/pc/department/edit";
	}
	
	/**
	 * 更新部门信息.
	 * @param department 部门信息
	 * @return String
	 */
	@RequestMapping("/updateDepartmentInfo.htm")
	@ResponseBody
	@Transactional
	public String updateDepartmentInfo(Department department) {
		User user = SecurityContextUtil.getCurrentUser();
		Department dept = this.departmentPcService.getEntity(department.getDepartmentId());
		dept.setCompanyId(department.getCompanyId());
		dept.setDepartmentName(department.getDepartmentName());
		dept.setDepartmentAddress(department.getDepartmentAddress());
		dept.setTelephone(department.getTelephone());
		dept.setFax(department.getFax());
		dept.setDisplayOrder(department.getDisplayOrder());
		dept.setModifyMan(user.getUserId());
		dept.setModifyTime(new Date());
		this.departmentPcService.updateEntity(dept);
		return "SUCCESS";
	}
	
	/**
	 * 根据部门名称查询部门信息.
	 * @param deptName 部门名称
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/search.htm")
	@ResponseBody
	@Transactional
	public List<Map<String, Object>> searchDept(String deptName) {
		return this.departmentPcService.searchDept(deptName);
	}
	
	/**
	 * 根据企业ID获取部门树.
	 * @param companyId 企业ID
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/deptComTree.htm")
	@ResponseBody
	public List<Map<String, Object>> getCombotree(String companyId) {
		return this.departmentPcService.deptTree(companyId);
	}
	
	/**
	 * 获取部门树-用户增加或修改部门.
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/combotree.htm")
	@ResponseBody
	public List<Map<String, Object>> getDeptTree() {
		String companyId = SecurityContextUtil.getCompanyId();
		List<Map<String, Object>> list =  this.departmentPcService.deptTree(companyId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", "0");
		map.put("text", "无");
		list.add(map);
		return list;
	}
	
	/**
	 * 根据部门ID删除部门.
	 * @param departmentIds 部门ID集合
	 * @return String
	 */
	@RequestMapping("/remove.htm")
	@ResponseBody
	@Transactional
	public String remove(String departmentIds) {
		String[] depts = departmentIds.split(",");
		for(int i = 0; i < depts.length; i++) {
			this.departmentPcService.deleteDepartment(depts[i]);
		}
		return "SUCCESS";
	}
	
	/**
	 * 根据企业ID获取其部门树.
	 * @param companyId 企业ID
	 * @return List<Map<String, Object>>
	 */
	@RequestMapping("/deptTree.htm")
	@ResponseBody
	public List<Map<String, Object>> getDeptTree(String companyId) {
		List<Map<String, Object>> list =  this.departmentPcService.deptTree(companyId);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", "0");
		map.put("text", "无");
		list.add(map);
		return list;
	}
	
	/**
	 * 根据部门ID获取其所属企业ID.
	 * @param departmentId 部门ID
	 * @return String
	 */
	@RequestMapping("/getCompanyId.htm")
	@ResponseBody
	public String getCompanyId(String departmentId) {
		Department department = this.departmentPcService.getEntity(departmentId);
		return StringUtils.hasText(department.getCompanyId()) ? department.getCompanyId() : "";
	}
}